$(function() {
	var login = '[login]';
	if (lang.login != undefined) {
		login = lang.login;
	}
	
	var create_account = '[create_account]';
	if (lang.create_account != undefined) {
		create_account = lang.create_account;
	}	
	$( "#login-form-dialog" ).dialog({
		autoOpen: false,
		height: 230,
		width: 400,
		modal: true,
		buttons: [
			{
				text: login,
				click: function() {
					$('#loginFrmValidation').hide();
					$( "#login-form-dialog" ).dialog({ height: 230 });
					if ($("#loginFrm").valid()){	
					$.ajax({
						url: settings.base_url + lang.lang_key + '/user/auth/',
						dataType: 'json',
						type: 'POST',
						data: {'email' : $('#email').val(), 'password' : $('#password').val()},
						success: function(data){
							if (!data.status) {
								$('#loginFrmValidation .message').html(data.message);
								$( "#login-form-dialog" ).dialog({ height: 260 });
								$('#loginFrmValidation').show();
								return false;
							}
							
							window.location.reload();
						},
						complete: function() {
						}
					});	
					}									
				}
			},
			{
				text: create_account,
				click: function() {
					window.location = settings.base_url + lang.lang_key + '/user/create/'
				}
			}			
		],
		close: function() {
			$('#loginFrmValidation').hide();
			$( "#login-form-dialog" ).dialog({ height: 230 });
		}		
	});

	$( "#loginBtn" ).click(function() {
        $.ajax({
            url: settings.base_url + lang.lang_key + '/user/login/',
            dataType: 'html',
            type: 'POST',
            success: function(html){
				$( "#login-form-dialog" ).html(html);
				$( "#login-form-dialog" ).removeClass('hidden');
				$("#loginFrm").validate({
				  rules: {
					email: {
					  required: true,
					  email:true
					},
					password: {
					  required: true
					}
				  },
				  event : 'blur'
				  });				
            },
            complete: function(html) {
				$( "#login-form-dialog" ).dialog( "open" );
            }
        });		
			
	});
});